<?php
extract(array(
	'extra_class' => '',
	'show_title' => true,
	'shorten_title' => 0,
	'show_image' => true,
	'image_width' => 110,
	'image_height' => 160,
	'show_numbered' => false,
	'show_author' => true,
	'show_rating' => true,
	'show_price' => true,
	'price_text' => '',
	'show_description' => false,
	'shorten_description' => 500,
	'show_share' => true,
	'show_buttons' => false,
	'show_buy_button' => false,
	'show_lend_button' => false,
	'show_borrow_button' => false,
	'custom_args' => false,
	'layout' => array('image', 'details' => 'open', 'title', 'author', 'rating', 'price', 'description', 'share', 'buttons', 'close'),
), EXTR_SKIP);
//load product promotion
if(!empty($product->product_id))
{
	$product->loadPromotion();
}
if(IS_MOBILE)//Mobile or Tablet
{
	$shorten_description = 200;
}
//End of check
if ( !empty($product) ) : ?>

<article class="product <?php echo $extra_class ?>" id="product_id_<?php echo !empty($product->product_id) ? $product->product_id : $product->user_id ;?>" itemscope itemtype="http://schema.org/Book">
	
	<?php foreach ($layout as $k => $l) {
		
		switch ($l) {
			case 'image':
				if ( $show_image ) 
				{
					if(!empty($product->product_id))
					{
						printf('<div class="thumbnail"><a class="" href="%s">', $product->url);
						$this->renderPartial('//common/image', array('pair' => $product->main_image_pair, 'object_type' => 'product', 'no_link' => true, 'width' => $image_width, 'height' => $image_height));
						printf('</a></div>');
					}
					else if(!empty($product->user_id))
					{
						printf('<div class="thumbnail"><a class="" href="%s">', $product->profileUrl);
						$this->renderPartial('//common/image', array('pair' => $product->avatar, 'object_type' => 'user', 'no_link' => true, 'width' => $image_width, 'height' => $image_height));
						printf('</a></div>');
					}
				}
				break;
				
			case 'image-nolink':
				if ( $show_image ) 
				{
					if(!empty($product->product_id))
					{
						printf('<div class="thumbnail">');
						$this->renderPartial('//common/image', array('pair' => $product->main_image_pair, 'object_type' => 'product', 'no_link' => true, 'width' => $image_width, 'height' => $image_height));
						printf('</div>');
					}
					else if(!empty($product->user_id))
					{
						printf('<div class="thumbnail">');
						$this->renderPartial('//common/image', array('pair' => $product->avatar, 'object_type' => 'user', 'no_link' => true, 'width' => $image_width, 'height' => $image_height));
						printf('</div>');
					}

				}
				break;
			
			case 'title':
				if ( $show_title ) {
					if(!empty($product->product_id))
					{
						$description_checked = (!empty($product->description) && !empty($product->description->name)) ? $product->description->name : "";
						printf('<h4 class="name"><a itemprop="name" href="%s" title="%s">', $product->url, $description_checked);
						if ( !empty($shorten_title) ) {
							echo shortenText($description_checked, $shorten_title);
						} else {
							echo $description_checked;
						}
						printf('</a></h4>');
					}
					else if(!empty($product->user_id))
					{
						printf('<h4 class="name"><a itemprop="name" href="%s" title="%s">', $product->profileUrl, $product->email);
						if ( !empty($shorten_title) ) {
							echo shortenText(userDisplayName($product), $shorten_title);
						} else {
							echo userDisplayName($product);
						}
						printf('</a></h4>');
					}
				}
				break;
				
			case 'title-h1':
				if ( $show_title ) 
				{
					if(!empty($product->product_id))
					{
						printf('<h1 class="name" itemprop="name">');
						if ( !empty($shorten_title) ) {
							echo shortenText($product->description->name, $shorten_title);
						} else {
							echo $product->description->name;
						}
						printf('</h1>');
					}
					else if(!empty($product->user_id))
					{
						printf('<h1 class="name" itemprop="name">');
						if ( !empty($shorten_title) ) {
							echo shortenText(userDisplayName($product), $shorten_title);
						} else {
							echo userDisplayName($product);
						}
						printf('</h1>');
					}
				}
				break;
			
			case 'author':
				//Kietnt: khong hien thi thong tin dien vien khi load vi rat cham
				/*
				if(!empty($product->product_id))
				{
					if ( $show_author && !empty($product->feature) && !empty($product->feature->authors) ) 
					{
						
						printf('<div class="author">');
						$this->renderPartial('//common/items_list', array(
							'items' => $product->feature->authors,
							'class' => 'author-name',
							'field' => 'name',
							'itemprop' => 'author',
							'urlPattern' => 'product/viewAuthor',
						));
						printf('</div>');
					}
				}
				else if(!empty($product->user_id))
				{
					printf('<div class="author">');
					printf('<div class="author" style="word-wrap: break-word;"><a title="%s" class="author-name" itemprop="author" href="%s">%s</a></div>', $product->email, $product->profileUrl, $product->email);
					printf('</div>');
				}
				*/
				if ($show_author && (!empty($product->addition_category) || !empty($product->main_category))) 
				{
					printf('<div class="author %s">', ($show_description) ? '' : 'x-item-floating');
					$this->renderPartial('//common/items_list', array(
						'items' => array($product->main_category),
						'sub_items' => $product->addition_category,
						'class' => 'author-name',
						'field' => 'category',
						'itemprop' => 'category',
						'title' => 'Thể loại',
						'id_field'=>'category_id',
						'urlPattern' => 'category/viewTheLoai',
					));
					printf('</div>');
				}
				if(!empty($product->video_year))
				{
					printf('<div class="author %s">', ($show_description) ? '' : 'x-item-floating');
					printf('Năm sản xuất: %s', $product->video_year);
					printf('</div>');
				}
				if(!empty($product->video_quality))
				{
					printf('<div class="author %s">', ($show_description) ? '' : 'x-item-floating');
					printf('Chất lượng phim: %s', $product->video_quality);
					printf('</div>');
				}
				break;
			
			case 'rating':
				if ( $show_rating ) 
				{
					if(!empty($product->product_id))
					{
						$rating = Comment::getRating($product->product_id, 'product');
						$this->renderPartial('//common/rating', array('rating' => $rating));
					}
				}
				break;
			
			case 'price':
				if(!empty($product->product_id))
				{
					if ( $show_price ) 
					{
	
						printf('<div class="price" itemprop="offers" itemscope itemtype="http://schema.org/Offer">');
						if ( !empty($price_text) ) echo $price_text;
						$this->renderPartial('//common/price', array('value' => $product->price, 'product' => $product, 'promotion' => ((empty($product->promotion_item)) ? 0 : $product->promotion_item->discount_value)));
						printf('</div>');
	
					}
				}
				break;
			
			case 'description':
				if(!empty($product->product_id))
				{
					if (!empty($product->description) && !empty($product->description->description) ) 
					{
						printf('<div class="description %s" itemprop="description">', ($show_description) ? '' : 'x-item-floating');
						$description = str_replace(array('<br />', '<br>', '<br/>'), ' ', $product->description->description);
						if ( !empty($shorten_description) ) {
							echo shortenText(strip_tags($description), $shorten_description);
						} else {
							echo strip_tags($description);
						}
						printf('</div>');
						
					}
				}
				break;
			
			case 'share':
				if ( $show_share ) {
					if(!empty($product->product_id))
					{
						$this->renderPartial('//common/share', array(
							'object_id' => $product->product_id,
							'object_type' => 'product',
						));
					}
				}
				break;
		
			case 'buttons':
				if(!empty($product->product_id)) {
					if ( $show_buttons ) {
						printf('<div class="buttons">');
						if ( $show_buy_button ) {
							$this->renderPartial('//buttons/player', array('product' => $product));
						} elseif ( $show_lend_button ) {
							$this->renderPartial('//buttons/lend', array('product' => $product));
						} elseif ( $show_borrow_button ) {
							$this->renderPartial('//buttons/borrow', array('product' => $product, 'user_id' => $user_id));
						}
						printf('</div>');
					}
				}
				break;
		
			case 'open':
				
				printf('<div class="%s">', $k);
				
				break;
			
			case 'close':
				
				printf('</div>');
				
				break;
			
			case 'numbered':
				if ( $show_numbered ) {
					
					printf('<div class="numbered"><span>%s</span></div>', $numbered);
					
				}				
				break;
				
			case 'details':
				if(!empty($product->product_id))
				{
					$this->renderPartial('//product/components/details', array(
						'product' => $product,
					));
				}
				break;
			
			case 'comments':
				if(!empty($product->product_id))
				{
					if ( !empty($product->comments) ) {
					
						$this->renderPartial('//comment/components/featured_comments', array(
							'product' => $product,
							'comments' => $product->featured_comments,
						));
						
					}
				}
				break;
				
			case 'custom':
				if(!empty($product->product_id))
				{
					$args = array('product' => $product);
					if ( !empty($custom_args) && !empty($custom_args[$k]) ) $args = array_merge($args, $custom_args[$k]);
					$this->renderPartial($k, $args);
				}
				break;
		}
		
	} ?>
	
</article>

<?php endif; ?>